//
//  WeaponDetailViewController.m
//  MilitaryDemo
//
//  Created by qianfeng01 on 15-7-8.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "WeaponDetailViewController.h"
#import "WeaponDatabase.h"
#import "WeaponDetail.h"
#import "SecondMOdel.h"
#import "WeaponDetail.h"
#import "UIImageView+WebCache.h"
#import "GDataXMLElement+util.h"
#import <WebKit/WebKit.h>
@interface WeaponDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>
{
    int fold[4];
}
@property(nonatomic,strong)WeaponDetail *detail;
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)UIWebView *webView1;
@property(nonatomic,strong)UIWebView *webView2;
@property(nonatomic,strong)UIWebView *webView3;
@property(nonatomic,strong)SecondMOdel *model;
@property(nonatomic,strong)UIScrollView *scrollView;

@end

@implementation WeaponDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    fold[0]=1;
    fold[1]=1;
    fold[2]=1;
    fold[3]=1;
     [self creatTableView];
    [self initModel];
}
-(void)creatTableView
{

    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64) style:UITableViewStyleGrouped];
    self.detail=[[[NSBundle mainBundle]loadNibNamed:@"WeaponDetail" owner:nil options:nil] lastObject];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.tableHeaderView=self.detail;
    [self.tableView registerNib:[UINib nibWithNibName:@"detailCell" bundle:nil] forCellReuseIdentifier:@"detailCell"];
    [self.view addSubview:self.tableView];
        self.webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, 1)];
        self.webView.delegate=self;
        self.webView.scrollView.scrollEnabled = NO;
        self.webView1=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width,1)];
        self.webView1.delegate=self;
        self.webView1.scrollView.scrollEnabled = NO;
        self.webView2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, 1)];
        self.webView2.delegate=self;
        self.webView2.scrollView.scrollEnabled = NO;
        self.webView3=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenSize.width, 1)];
        self.webView3.delegate=self;
    self.webView3.scrollView.scrollEnabled = NO;

}

-(void)initModel
{
    self.dataArr=[[WeaponDatabase sharedManager]selectAllatID:self.id];
    self.model=self.dataArr[0];
    [self.detail.iconImageView sd_setImageWithURL:[NSURL URLWithString:self.model.pic]placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    self.detail.titleLabel.text=[NSString stringWithFormat:@"%@简介",self.title];
    self.detail.decLabel.text=self.model.des;
    
    NSArray *arr=@[self.model.develop,self.model.set,self.model.belong,self.model.comment];
    [self.webView loadHTMLString:arr[0] baseURL:nil];
    [self.webView1 loadHTMLString:arr[1] baseURL:nil];
    [self.webView2 loadHTMLString:arr[2] baseURL:nil];
    [self.webView3 loadHTMLString:arr[3] baseURL:nil];
    [self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return fold[section]==0?1:0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            static NSString *cellId=@"cellID";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell==nil) {
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            }
            [cell addSubview:self.webView];
            return cell;
        }
            break;
        case 1:
        {
            static NSString *cellId=@"cell";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell==nil) {
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            }
            [cell addSubview:self.webView1];
            return cell;
        }
            break;
        case 2:
        {
            static NSString *cellId=@"cel";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell==nil) {
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            }

            [cell addSubview:self.webView2];
             return cell;
        }
            break;
        case 3:
        {
            static NSString *cellId=@"ce";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell==nil) {
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            }

            [cell addSubview:self.webView3];
             return cell;
        }
            break;
        default:
            break;
    }

         return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            return _webView.frame.size.height;
        
        }
            break;
        case 1:
        {
            return _webView1.frame.size.height;
        }
            break;
        case 2:
        {
            return _webView2.frame.size.height;
        }
            break;
        case 3:
        {
            return _webView3.frame.size.height;
        }
            break;
        default:
            break;
    }
    return 0;
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect frame = [webView frame];
    frame.size.height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] integerValue]-10;
    [webView setFrame:frame];
    [self.tableView reloadData];
    //通知tableView reload包含WebView的那个Cell, 调整cell的height
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 40)];
        view.backgroundColor=[UIColor darkGrayColor];
        view.tag = section + 101; // 101 102 103 104 105
        NSString *imageName = fold[section] == 0 ? @"unfold.png" : @"fold.png";
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.center = CGPointMake(view.frame.size.width-40, view.frame.size.height/2.0);
        imageView.tag = 10;
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 40)];
        switch (section) {
            case 0:
            {
                label.text=@"研发历程";
            }
                break;
            case 1:
            {
                label.text=@"装置配置";
            }
                break;
            case 2:
            {
                label.text=@"服役部队";
            }
                break;
            case 3:
            {
                label.text=@"媒体评述";
            }
                break;
                
            default:
                break;
        }
        [view addSubview:label];
        [view addSubview:imageView];
        // 把点击手势加在头视图上面
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnHeaderView:)];
        [view addGestureRecognizer:tap];
        return view;
   }

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40.0;
}
- (void)tapOnHeaderView:(UITapGestureRecognizer *)tap {
    UIView *headerView = tap.view; // 取到我们点击的是哪个view
    NSInteger section = headerView.tag - 101;
    fold[section] ^= 1;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
     //[self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
